package business;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.ListIterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.IndividualClassDAO;
import dao.TeacherDAO;
import orm.IndividualClass;
import orm.Teacher;

import util.DBHelper;
import util.Utility;
public class IndividualClassInfoServlet extends HttpServlet {

	/**
	 * TeacherInfoServlet
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public IndividualClassInfoServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String strFunc = request.getParameter("func");
		int PageSize = Utility.PageSize;
		Connection con = null;
		Statement stmt = null;
		if (strFunc.equals("load")) {
			loadFirstPage(request ,response);
			request.getRequestDispatcher("../" + Utility.JSP_IndividualClassInfo).forward(request, response);
		} else if (strFunc.equals("edit")) {
			String IClassID = request.getParameter("IClassID");
			IndividualClassDAO individualClassDAO = new IndividualClassDAO();
			
			try {
				IndividualClass individualClass = individualClassDAO.getIClassID(IClassID);
				request.setAttribute("individualClass", individualClass);
				request.getRequestDispatcher("../" + Utility.JSP_IndividualClassEdit).forward(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if (strFunc.equals("delete")) {
			String IClassID = request.getParameter("IClassID");
			try {
				con = DBHelper.getConnection();
				con.setAutoCommit(false);
				stmt = con.createStatement();
				
				IndividualClassDAO individualClassDAO = new IndividualClassDAO();
				IndividualClass individualClass = individualClassDAO.getIClassID(IClassID);

				individualClassDAO.deleteIndividualClass(stmt, IClassID);

				con.commit();
				stmt.close();
				
				ArrayList al = null;
				al = new ArrayList<IndividualClass>();
				ListIterator<IndividualClass> it = individualClassDAO.getAllIndividualClasss().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					IndividualClass t = it.next();
					IndividualClass t1 = new IndividualClass();
					t1.setIClassID(t.getIClassID());
					t1.setIClassName(t.getIClassName());
					t1.setTeacherName(t.getTeacherName());
					t1.setClassTerm(t.getClassTerm());
					t1.setAllocation(t.getAllocation()-1);
					
					al.add(t1);
					ii++;
					if(ii > (PageSize-1) ) {
						break;
					}
					
				}
				
				String tmp = request.getParameter("HDPageNo");
				int nPageNo = Integer.parseInt(tmp); 
				ArrayList<IndividualClass> alIndividualClass = individualClassDAO.getAllIndividualClasss();
				int totalPage = 0;
				if (alIndividualClass.size() % PageSize == 0) {
					totalPage = alIndividualClass.size()/PageSize;
					request.setAttribute("totalPage",totalPage);
				} else {
					totalPage = alIndividualClass.size()/PageSize + 1;
					request.setAttribute("totalPage",totalPage);
				}
				
				
				request.setAttribute("pageNo", 1);
				
				request.setAttribute("individualClassList", al);
				request.getRequestDispatcher("../" + Utility.JSP_IndividualClassInfo).forward(request, response);
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
				try {
					con.rollback();
				} catch (SQLException ee) {
					ee.printStackTrace();
				}
			}
		} else if (strFunc.equals("paging")) {
			paging(request, response);
			
			request.getRequestDispatcher("../" + Utility.JSP_IndividualClassInfo).forward(request, response);
		} else if (strFunc.equals("searchPage")) {
			searchPage(request, response);
			
			request.getRequestDispatcher("../" + Utility.JSP_IndividualClassInfo).forward(request, response);
		} else if (strFunc.equals("searchIndividualClassInfo")) {
			loadFirstPage(request ,response);
			request.getRequestDispatcher("../" + Utility.JSP_SearchIndividualClassInfo).forward(request, response);
		} else if (strFunc.equals("searchIndividualClassToPaging")) {
			paging(request, response);
			request.getRequestDispatcher("../" + Utility.JSP_SearchIndividualClassInfo).forward(request, response);
		} else if (strFunc.equals("searchIndividualClassToSearchPage")) {
			searchPage(request, response);
			request.getRequestDispatcher("../" + Utility.JSP_SearchIndividualClassInfo).forward(request, response);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	public void loadFirstPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int PageSize = Utility.PageSize;
		IndividualClassDAO individualClassDAO = new IndividualClassDAO();
		try {
			request.setAttribute("pageNo", 1);
			ArrayList<IndividualClass> al = individualClassDAO.getAllIndividualClasss();
			if (al.size() % PageSize == 0) {
				request.setAttribute("totalPage",al.size()/PageSize);
			} else {
				request.setAttribute("totalPage",(al.size()/PageSize) + 1);
			}
			
			al = new ArrayList<IndividualClass>();
			ListIterator<IndividualClass> it = individualClassDAO.getAllIndividualClasss().listIterator();
			int ii = 0;
			while (it.hasNext()) {
				IndividualClass t = it.next();
				IndividualClass t1 = new IndividualClass();
				t1.setIClassID(t.getIClassID());
				t1.setIClassName(t.getIClassName());
				t1.setTeacherName(t.getTeacherName());
				t1.setClassTerm(t.getClassTerm());
				t1.setAllocation(t.getAllocation()-1);
				
				al.add(t1);
				ii++;
				if(ii > (PageSize-1) ) {
					break;
				}
				
			}
			
			request.setAttribute("individualClassList", al);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void paging(HttpServletRequest request, HttpServletResponse response){
		String strSub = request.getParameter("sub");
		int PageSize = Utility.PageSize;
		String tmp = request.getParameter("HDPageNo");
		int nPageNo = Integer.parseInt(tmp); 
		
		IndividualClassDAO individualClassDAO = new IndividualClassDAO();
		try {
//			request.setAttribute("pageNo", nPageNo);
			ArrayList<IndividualClass> al = individualClassDAO.getAllIndividualClasss();
			int totalPage = 0;
			if (al.size() % PageSize == 0) {
				totalPage = al.size()/PageSize;
				request.setAttribute("totalPage",totalPage);
			} else {
				totalPage = al.size()/PageSize + 1;
				request.setAttribute("totalPage",totalPage);
			}
			
			if (strSub == null)
			{
				
			} else if (strSub.equals("start")) {
				nPageNo = 1;
			} else if (strSub.equals("front")) {
				nPageNo--;
			} else if (strSub.equals("next")) {
				nPageNo++;
			} else if (strSub.equals("last")) {
				nPageNo = totalPage ;
			} 
			request.setAttribute("pageNo", nPageNo);
			al = new ArrayList<IndividualClass>();
			ListIterator<IndividualClass> it = individualClassDAO.getAllIndividualClasss().listIterator();
			int ii = 0;
			while (it.hasNext()) {
				ii++;
				IndividualClass t = it.next();
				if (ii <= PageSize * (nPageNo - 1))
					continue;
				
				IndividualClass t1 = new IndividualClass();
				t1.setIClassID(t.getIClassID());
				t1.setIClassName(t.getIClassName());
				t1.setTeacherName(t.getTeacherName());
				t1.setClassTerm(t.getClassTerm());
				t1.setAllocation(t.getAllocation()-1);
				
				al.add(t1);
				
				if(ii > PageSize * nPageNo - 1 ) {
					break;
				}
			}
			
			request.setAttribute("individualClassList", al);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void searchPage(HttpServletRequest request, HttpServletResponse response){
		String strSub = request.getParameter("sub");
		int PageSize = Utility.PageSize;
		String tmp = request.getParameter("PageNo");
		int nPageNo = Integer.parseInt(tmp);

		IndividualClassDAO individualClassDAO = new IndividualClassDAO();
		try {
//			request.setAttribute("pageNo", nPageNo);
			ArrayList<IndividualClass> al = individualClassDAO.getAllIndividualClasss();
			int totalPage = 0;
			if (al.size() % PageSize == 0) {
				totalPage = al.size()/PageSize;
				request.setAttribute("totalPage",totalPage);
			} else {
				totalPage = al.size()/PageSize + 1;
				request.setAttribute("totalPage",totalPage);
			}
			
			request.setAttribute("pageNo", nPageNo);
			al = new ArrayList<IndividualClass>();
			ListIterator<IndividualClass> it = individualClassDAO.getAllIndividualClasss().listIterator();
			int ii = 0;
			while (it.hasNext()) {
				ii++;
				IndividualClass t = it.next();
				if (ii <= PageSize * (nPageNo - 1))
					continue;
				
				IndividualClass t1 = new IndividualClass();
				t1.setIClassID(t.getIClassID());
				t1.setIClassName(t.getIClassName());
				t1.setTeacherName(t.getTeacherName());
				t1.setClassTerm(t.getClassTerm());
				t1.setAllocation(t.getAllocation()-1);
				
				al.add(t1);
				
				if(ii > PageSize * nPageNo - 1 ) {
					break;
				}
			}
			
			request.setAttribute("individualClassList", al);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
